<template>
  <div class="app-container">
    <el-card>
      <el-form ref="searchForm" :model="searchForm" inline size="small">
        <el-form-item label="帖子标题">
          <el-input
            v-model="searchForm.title"
            placeholder="请输入帖子标题"
            clearable
          />
        </el-form-item>
        <el-form-item label="发布时间" prop="startTime">
          <el-date-picker
            style="width: 200px"
            v-model="searchForm.beginTime"
            align="right"
            type="date"
            placeholder="选择开始日期"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd"
            format="yyyy 年 MM 月 dd 日"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>-</el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="searchForm.endTime"
            align="right"
            type="date"
            placeholder="选择结束日期"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd"
            format="yyyy 年 MM 月 dd 日"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="作者Id">
          <el-input
            v-model="searchForm.memberId"
            placeholder="请输入作者Id"
            clearable
          />
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select
            v-model="searchForm.verifyStatus"
            placeholder="请选择审核状态"
            clearable
            size="small"
          >
            <el-option label="待审核" value="0"></el-option>
            <el-option label="审核通过" value="1"></el-option>
            <el-option label="审核未通过" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh" @click="resetForm('searchForm')"
            >重置</el-button
          >
          <el-button icon="el-icon-search" type="primary" @click="getList()"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div>
        <el-button size="small" @click="deleteMany()" icon="el-icon-delete"
          >批量删除</el-button
        >
      </div>

      <el-table
        :data="tableData.data"
        v-loading="listLoading"
        border
        style="width: 100%; margin-top: 20px"
        @selection-change="selectTableList"
        size="small"
      >
        <el-table-column align="center" type="selection" width="55" />
        <el-table-column align="center" prop="id" label="ID" width="80" />
        <el-table-column align="center" prop="title" label="标题" />
        <el-table-column align="center" prop="nickname" label="作者" />
        <el-table-column align="center" prop="memberId" label="作者id" />

        <el-table-column align="center" prop="publishTime" label="发帖时间" />
        <el-table-column
          align="center"
          prop="visitCount"
          label="阅读量"
        ></el-table-column>

        <el-table-column
          prop="likeCount"
          align="center"
          label="点赞"
        ></el-table-column>
        <el-table-column
          prop="collectCount"
          align="center"
          label="收藏"
        ></el-table-column>
        <el-table-column align="center" label="评论">
          <template slot-scope="scope">
            <router-link :to="{ path: `/content/talk?id=${scope.row.id}` }">
              <span style="cursor: pointer; color: #3a8ee6">{{
                scope.row.commentCount
              }}</span>
            </router-link>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <!--            verifyStatus 审核状态：0->未审核；1->审核通过；2->审核不通过-->
            <el-tag type="info" size="mini" v-if="scope.row.verifyStatus == 0"
              >待审核</el-tag
            >
            <el-tag type="danger" size="mini" v-if="scope.row.verifyStatus == 2"
              >审核未通过</el-tag
            >
            <el-tag
              type="success"
              size="mini"
              v-if="scope.row.verifyStatus == 1"
              >审核通过</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column align="center" prop="verifyTime" label="审核时间" />
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="allpyDetail(scope.row)" type="text"
              >审核</el-button
            >

            <el-button size="mini" type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        class="pageRight"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 40]"
        :current-page="searchForm.pageNum"
        :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { pickerOptions } from "../../../product/options";
import {
  getArticleList,
  deleteArticle,
  editArticleStatus,
  deleteArticleBatch,
  editArticleTop
} from "@/api/article";
import Tinymce from "@/components/Tinymce/index.vue";
export default {
  name: "Article",
  components: {
    Tinymce
  },
  data() {
    return {
      props: {
        emitPath: false
      },
      pickerOptions,
      str: "",
      searchForm: {
        memberId: "",
        verifyStatus: "",
        sourceType: 1,
        title: "",
        beginTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 10
      },
      tableData: {
        data: [],
        total: 0
      },
      listLoading: false,
      deleteList: [],
      cateList: []
    };
  },
  async created() {
    await this.getList();
  },
  mounted() {},
  methods: {
    allpyDetail(v) {
      this.$router.push({
        path: "/content/postDetail",
        query: {
          id: v.id
        }
      });
    },

    resetForm(form) {
      // this.$refs[form].resetFields();
      this.searchForm = {
        memberId: "",
        verifyStatus: "",
        sourceType: 1,
        title: "",
        beginTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 10,
        cateId: ""
      };
      this.getList();
    },
    selectTableList(val) {
      this.deleteList = val.map(v => v.id);
    },

    deleteMany() {
      let that = this;
      if (this.deleteList.length == 0) {
        this.$message({
          message: "请先选择要删除的项",
          type: "warning"
        });
      } else {
        this.$confirm("是否确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          deleteArticleBatch(this.deleteList.join(",")).then(res => {
            if (res.code == 200) {
              that.$message({
                type: "success",
                message: "删除成功!"
              });
              setTimeout(() => {
                that.getList();
              }, 500);
            }
          });
        });
      }
    },
    changeStatus(row) {
      editArticleStatus({
        articleIdList: [row.id],
        publishStatus: 0
      }).then(res => {
        this.$message({
          type: "success",
          message: "修改成功!"
        });
        this.getList();
      });
    },
    handleEdit(v) {
      this.$router.push({
        path: "/article/add",
        query: {
          id: v.id
        }
      });
    },
    handleDelete(v) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteArticle(v.id).then(res => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.getList();
        });
      });
    },
    handleDetail(v) {
      console.log("详情");
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val;
      this.getList();
    },
    addArtice() {
      this.$router.push({
        path: "/article/add"
      });
    },
    getList() {
      this.listLoading = true;
      getArticleList(this.searchForm)
        .then(res => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.total;
          this.listLoading = false;
        })
        .finally(() => {
          this.listLoading = false;
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.app-container {
  height: calc(100vh - 50px);
  background-color: #f5f5f5;
  position: relative;
}
</style>
